//
//  MBDeliverGoodsPopView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBDeliverGoodsPopView.h"
#import "MBDeliverItemView.h"
#import "MBShippingPopView.h"

@interface MBDeliverGoodsPopView ()
@property (nonatomic,weak) UIView * backShadowView;
@property (nonatomic,weak) UIView * containView;

@property (nonatomic,weak)MBDeliverItemView* numItemView;
@property (nonatomic,weak)MBDeliverItemView* timeItemView;
@property (nonatomic,weak)MBDeliverItemView* moneyItemView;
@property (nonatomic,weak)MBDeliverItemView* nameItemView;
@property (nonatomic,weak)MBDeliverItemView* addressItemView;
@property (nonatomic,weak)MBDeliverItemView* phoneItemView;

@property (nonatomic,weak)MBShippingPopView* shippingPopView;
@property (nonatomic,weak)UIImageView* chooseImageView;
@property (nonatomic,weak)UILabel* shippingLabel;
@property (nonatomic,weak)UIButton* shureButton;


@property (nonatomic,assign)BOOL isShowShippingView;
@end

@implementation MBDeliverGoodsPopView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        //创建shadowView
        UIView * backShadowView = [[UIView alloc] init];
        backShadowView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        backShadowView.alpha = 0.0;
        [self addSubview:backShadowView];
        self.backShadowView = backShadowView;
        
        [backShadowView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
        
        [backShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsZero);
        }];
        
        UIView * containView = [[UIView alloc] initWithFrame:CGRectMake([UIView lf_sizeFromIphone6:35], SCREEN_HEIGHT, SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80], [UIView lf_sizeFromIphone6:390])];
        containView.backgroundColor = [UIColor whiteColor];
        [self addSubview:containView];
        self.containView = containView;
        
        UIScrollView* scrollView = [[UIScrollView alloc]init];
        [containView addSubview:scrollView];
        
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(containView).insets(UIEdgeInsetsZero);
        }];
        
        MBDeliverItemView* numItemView = [[MBDeliverItemView alloc]initWithTitle:@"订单编号：" andMessage:@"32156586365965632"];
        self.numItemView = numItemView;
        [scrollView addSubview:numItemView];
        
        [numItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:26]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];

        MBDeliverItemView* timeItemView = [[MBDeliverItemView alloc]initWithTitle:@"创建时间：" andMessage:@"2018-02-12 15:25:10"];
        self.timeItemView = timeItemView;
        [scrollView addSubview:timeItemView];
        
        [timeItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(numItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];

        MBDeliverItemView* moneyItemView = [[MBDeliverItemView alloc]initWithTitle:@"订单总价：" andMessage:@"￥1250.20"];
        self.moneyItemView = moneyItemView;
        moneyItemView.messageLabel.textColor = [UIColor colorWithR:255 G:0 B:7];
        [scrollView addSubview:moneyItemView];
        
        [moneyItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(timeItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];
        
        UIView* line = [[UIView alloc]init];
        line.backgroundColor = [UIColor colorWithR:241 G:241 B:241];
        [scrollView addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(moneyItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:17]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:1]);
        }];
        
        MBDeliverItemView* nameItemView = [[MBDeliverItemView alloc]initWithTitle:@"收货人：" andMessage:@"三小"];
        self.nameItemView = nameItemView;
        [scrollView addSubview:nameItemView];
        
        [nameItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(line.mas_bottom).offset([UIView lf_sizeFromIphone6:17]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];

        MBDeliverItemView* addressItemView= [[MBDeliverItemView alloc]initWithTitle:@"收货地址：" andMessage:@"   江苏省苏州市工业园区娄葑镇若水路388号纳米技术大学E楼E206（掌心信息技术有限公司）"];
        self.addressItemView = addressItemView;
        [scrollView addSubview:addressItemView];
        
        [addressItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nameItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];

        MBDeliverItemView* phoneItemView = [[MBDeliverItemView alloc]initWithTitle:@"电话：" andMessage:@"15111454454"];
        self.phoneItemView = phoneItemView;
        [scrollView addSubview:phoneItemView];
        
        [phoneItemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(addressItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:17]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
        }];

        UIView* line2 = [[UIView alloc]init];
        line2.backgroundColor = [UIColor colorWithR:241 G:241 B:241];
        [scrollView addSubview:line2];
        
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(phoneItemView.mas_bottom).offset([UIView lf_sizeFromIphone6:17]);
            make.left.equalTo(scrollView);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:80]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:1]);
        }];
        
        UILabel* logisticsNameLabel = [[UILabel alloc]init];
        logisticsNameLabel.text = @"配送物流：";
        logisticsNameLabel.font = [UIFont lf_systemFontOfSize:12];
        logisticsNameLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [scrollView addSubview:logisticsNameLabel];
        
        [logisticsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(line2.mas_bottom).offset([UIView lf_sizeFromIphone6:18]);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:12]);
        }];

        UIView* chooseShippingView = [[UIView alloc]init];
        chooseShippingView.layer.borderColor = [UIColor colorWithR:216 G:216 B:216].CGColor;
        chooseShippingView.layer.borderWidth = [UIView lf_sizeFromIphone6:1];
        [chooseShippingView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleChooseShipping)]];
        [scrollView addSubview:chooseShippingView];

        UILabel* shippingLabel = [[UILabel alloc]init];
        self.shippingLabel = shippingLabel;
        shippingLabel.text = @"请选择物流";
        shippingLabel.font = [UIFont lf_systemFontOfSize:12];
        shippingLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [chooseShippingView addSubview:shippingLabel];
        
        [shippingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(chooseShippingView);
            make.left.equalTo(chooseShippingView).offset([UIView lf_sizeFromIphone6:5]);
        }];

        UIImageView* chooseImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"day_un_choose"]];
        self.chooseImageView = chooseImageView;
        [chooseShippingView addSubview:chooseImageView];
        
        [chooseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(chooseShippingView);
            make.right.equalTo(chooseShippingView).offset([UIView lf_sizeFromIphone6:-5]);
        }];
        
        [chooseShippingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(logisticsNameLabel);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:74]);
            make.width.mas_equalTo([UIView lf_sizeFromIphone6:213]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:26]);
        }];
        
        
        UILabel* logisticsNumLabel = [[UILabel alloc]init];
        logisticsNumLabel.text = @"配送单号：";
        logisticsNumLabel.font = [UIFont lf_systemFontOfSize:12];
        logisticsNumLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [scrollView addSubview:logisticsNumLabel];
        
        [logisticsNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(chooseShippingView.mas_bottom).offset([UIView lf_sizeFromIphone6:22]);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:12]);
        }];
        
        UITextField* logisticsNumTextField = [[UITextField alloc]init];
        self.logisticsNumTextField = logisticsNumTextField;
        logisticsNumTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIView lf_sizeFromIphone6:5], 0)];
        logisticsNumTextField.leftViewMode = UITextFieldViewModeAlways;
        logisticsNumTextField.placeholder = @"填写运单号";
        logisticsNumTextField.font = [UIFont lf_systemFontOfSize:12];
        logisticsNumTextField.textColor = [UIColor colorWithR:24 G:24 B:24];
        logisticsNumTextField.layer.borderColor = [UIColor colorWithR:216 G:216 B:216].CGColor;
        logisticsNumTextField.layer.borderWidth = [UIView lf_sizeFromIphone6:1];
        [scrollView addSubview:logisticsNumTextField];
        
        [logisticsNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(logisticsNumLabel);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:74]);
            make.width.mas_equalTo([UIView lf_sizeFromIphone6:213]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:26]);
        }];

        UIButton* shureButton = [[UIButton alloc]init];
        self.shureButton = shureButton;
        [shureButton setBackgroundImage:[UIImage imageFromColor:COLOR_MAIN forSize:CGSizeMake(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:108], [UIView lf_sizeFromIphone6:30]) withCornerRadius:[UIView lf_sizeFromIphone6:4]] forState:UIControlStateNormal];
        [shureButton setTitle:@"确认发货" forState:UIControlStateNormal];
        shureButton.titleLabel.font = [UIFont lf_systemFontOfSize:14];
        [scrollView addSubview:shureButton];
        
        [shureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(logisticsNumTextField.mas_bottom).offset([UIView lf_sizeFromIphone6:25]);
            make.left.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:54]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:196], [UIView lf_sizeFromIphone6:30]));
            make.bottom.equalTo(scrollView).offset([UIView lf_sizeFromIphone6:-34]);
        }];
        
        self.isShowShippingView = NO;
        MBShippingPopView* shippingView = [[MBShippingPopView alloc]init];
        shippingView.backgroundColor = [UIColor whiteColor];
        self.shippingPopView = shippingView;
        shippingView.layer.borderColor = [UIColor colorWithR:216 G:216 B:216].CGColor;
        shippingView.layer.borderWidth = [UIView lf_sizeFromIphone6:1];
        [scrollView addSubview:shippingView];
        
        [shippingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(chooseShippingView.mas_bottom);
            make.left.right.equalTo(chooseShippingView);
            make.height.mas_equalTo(0);
        }];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];

        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [[UIApplication sharedApplication].keyWindow sendSubviewToBack:self];

        [self recValue];
    }
    return self;
}

-(MBOrderConfirmParam *)confirmParam
{
    if (_confirmParam == nil) {
        _confirmParam = [[MBOrderConfirmParam alloc]init];
        _confirmParam.order_id = self.orderModel.order_id;
        MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
        _confirmParam.store_id = userModel.store_id;
        _confirmParam.seller_id = userModel.seller_id;
        _confirmParam.token = userModel.token;
    }
    return _confirmParam;
}

-(void)recValue
{
    @weakify(self);
    [RACObserve(self.shippingPopView, chooseShippModel)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if (self.shippingPopView.chooseShippModel) {
            [self handleChooseShipping];
            self.shippingLabel.text = self.shippingPopView.chooseShippModel.name;
            self.confirmParam.shipping_code = self.shippingPopView.chooseShippModel.code;
            self.confirmParam.shipping_name = self.shippingPopView.chooseShippModel.name;
        }
    }];
    
    [[self.shureButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.shippingPopView.chooseShippModel) {
            if (self.logisticsNumTextField.text.length == 0) {
                 [InterfaceHUD showErrorHUDWithString:@"请输入物流号！"];
            }else {
                self.shureSendGoods = YES;
            }
        }else {
            self.shureSendGoods = YES;
        }
    }];
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 1.0;
        self.containView.y = (SCREEN_HEIGHT - self.containView.height)/2;
    }];
}

- (void)close
{
    [self endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 0.0;
        self.containView.y = SCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication].keyWindow sendSubviewToBack:self];
    }];
}

-(void)keyboardWillShow:(NSNotification*)notifi
{
    NSDictionary* info = notifi.userInfo;
    CGRect kbFrame=[[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 1.0;
        self.containView.y = SCREEN_HEIGHT - kbFrame.size.height-self.containView.height;
    }];

}

-(void)keyboardWillHidden
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backShadowView.alpha = 1.0;
        self.containView.y = (SCREEN_HEIGHT - self.containView.height)/2;
    }];
}

-(void)handleChooseShipping
{
    if (!self.isShowShippingView) {
        self.isShowShippingView = YES;
        self.chooseImageView.image = [UIImage imageNamed:@"day_choose"];
        [self.shippingPopView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:130]);
        }];
    }else {
        self.isShowShippingView = NO;
        self.chooseImageView.image = [UIImage imageNamed:@"day_un_choose"];
        [self.shippingPopView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

-(void)setOrderModel:(MBOrderModel *)orderModel
{
    _orderModel = orderModel;
    
    self.numItemView.messageLabel.text = orderModel.order_sn;
    self.timeItemView.messageLabel.text = orderModel.add_time;
    self.moneyItemView.messageLabel.text = [NSString stringWithFormat:@"￥%@",orderModel.order_amount];
    
    self.nameItemView.messageLabel.text = orderModel.consignee;
    self.addressItemView.messageLabel.text = orderModel.address;
    self.phoneItemView.messageLabel.text = orderModel.mobile;
}

@end
